package com.ybt.heartratem.common.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ybt.heartratem.R;

/**
 * 选择图片操作弹出框
 *
 * @author tony.liu
 */
public class OperateSelectPicDialog extends Dialog {
    private Context mContext;
    private View mainView;
    private OperatorListener listener;
    private TextView mType1TV, mType2TV, mCancelTV;

    public OperateSelectPicDialog(Context context) {
        super(context, R.style.Theme_Hold_Dialog_Base);
        setCanceledOnTouchOutside(true);
        mContext = context;
        init();
    }

    public OperateSelectPicDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setCanceledOnTouchOutside(true);
        init();
    }

    protected OperateSelectPicDialog(Context context, boolean cancelable,
                                     OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
        setCanceledOnTouchOutside(true);
        init();
    }

    private void init() {
        mainView = LayoutInflater.from(mContext).inflate(R.layout.dialog_select_pic_layout, null);
        setContentView(mainView);
        mCancelTV = mainView.findViewById(R.id.cancel_tv);

        mType1TV = mainView.findViewById(R.id.type_1_tv);
        mType2TV = mainView.findViewById(R.id.type_2_tv);
        mCancelTV.setOnClickListener(view -> {
            dismiss();
            cancel();
        });

        mType1TV.setOnClickListener(view -> {
            listener.operator(1);
            dismiss();
        });
        mType2TV.setOnClickListener(view -> {
            listener.operator(2);
            dismiss();
        });
    }

    @Override
    public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.DialogPopupAnimation);
        super.show();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = 700;
        window.setAttributes(lp);
    }


    public void setItem(OperatorListener listener) {
        this.listener = listener;
    }

    public interface OperatorListener {
        void operator(int key);
    }
}
